<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm">
                    <el-form-item label="标题" :label-width="formLabelWidth">
                        <el-input v-model="addForm.title" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="内容" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.content" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="是否隐藏" :label-width="formLabelWidth">
                        <el-switch v-model="show" active-color="#13ce66">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="所属机构" :label-width="formLabelWidth">
                        <el-select v-model="addForm.organizationId" placeholder="请选择所属机构">
                            <el-option v-for="(org,index) in orgs" :key="index" :label="org.name" :value="org.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文件" :label-width="formLabelWidth">
                        <el-upload class="upload-demo" :action="getLocation" :data="uploadVideo" ref="upload" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess" :limit="1" :on-exceed="handleExceed" :file-list="fileList">
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入标题" class="search"></el-input>
            <el-date-picker v-model="time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="addEx">新增视频</el-button>
        </div>
        <div>
            <el-table :data="videos.content" border fit style="width: 100%">
                <el-table-column label="视频">
                    <template slot-scope="scope">
                        <video :src="scope.row.videoUrl" controls width="100%" height="100%">
                            你的浏览器不支持
                            <code>video</code> 标签
                        </video>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="标题" show-overflow-tooltip>
                </el-table-column>

                <el-table-column prop="content" label="内容" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="createTime" label="添加时间">
                </el-table-column>

                <el-table-column prop="organizationId" label="所属机构" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="shield" :formatter="formatStatus" label="状态">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editPic(scope.row)" size="small">编辑</el-button>
                        <el-button type="text" @click="delPic(scope.row)" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="videos.number+1" layout="total, sizes, prev, pager, next, jumper" :total="videos.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { addVideo, delVideo, putVideo, allVideo } from '../../server/getDate.js'
import { mapState, mapActions } from 'vuex'
import { location } from '../../utils/local.js'
import { formatDate } from '../../utils/date.js'
export default {
    data() {
        return {
            page: {},
            addForm: {},
            dialogFormVisible: false,
            title: '',
            getLocation: '',
            formLabelWidth: '120px',
            uploadVideo: {},
            show: false,
            fileList: [],
            select_word: '',
            time: ''
        }
    },
    created() {
        this.getAllVideos()
        this.getLocation = location
    },
    computed: {
        ...mapState({
            videos: state => state.File.videos,
            orgs: state => state.Org.org,
            user: state => state.Login.user
        })
    },
    methods: {
        ...mapActions(['getVideos']),
        getAllVideos() {
            allVideo().then(res => {
                if (res.status === 0) {
                    this.getVideos(res.result)
                } else {
                    this.$message.error(res.message)
                }
            }, err => this.$message.error('服务器异常'))
        },
        add() {
            let videoEntity = this.addForm
            let userId = this.user.id
            console.log('this.show', this.show)
            videoEntity.shield = this.show ? 0 : 1
            if (videoEntity.id) {
                putVideo({ videoEntity, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.$message.info('修改成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            } else {
                console.log('add', videoEntity)
                addVideo({ videoEntity, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.$message.info('增加成功')
                            this.getAllVideos()
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }
        },
        addEx() {
            this.dialogFormVisible = true
            this.title = '添加视频'
            this.addForm = {}
            this.fileList = []
        },
        editPic(row) {
            console.log('row', row)
            this.dialogFormVisible = true
            this.title = '修改视频'
            this.addForm = row
            this.show = row.shield == 1 ? false : true
        },
        delPic(row) {
            let id = row.id
            let userId = this.user.id
            delVideo({ id, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.$message.info('删除成功')
                        this.getAllVideos()
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常'))

        },
        formatStatus(row, col) {
            return row.shield == 1 ? '展示' : "不展示"
        },
        beforeAvatarUpload(file) {
            console.log('上传视频', file)
            return this.upload(file)
        },
        handleAvatarSuccess(res, file) {
            this.addForm.videoUrl = res.result
            console.log('addForm', this.addForm)
        },
        upload(file) {
            this.uploadVideo.type = 1
            this.uploadVideo.file = file
            console.log('upload', this.uploadVideo)
        },
        handleCurrentChange(val) {
            console.log('val', val)
            let index = val
            let field = "title"
            let keyword = this.select_word
            let beginTime = ''
            let endTime = ''
            if (this.time) {
                beginTime = formatDate(this.time[0], 'yyyy-MM-dd')
                endTime = formatDate(this.time[1], 'yyyy-MM-dd')
            }
            let size = this.page.size
            allVideo({ index, size, field, keyword, beginTime, endTime })
                .then(res => {
                    if (res.status === 0) {
                        this.getVideos(res.result)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常')

                )
        },
        handleExceed(files, filelist) {
            console.log('handleExceed', files, filelist)

        },
        handleRemove(file, filelist) {
            console.log('remove', file, filelist)
            this.fileList = filelist
        },
        handlePreview(file) {
            console.log('preview', file)
        },
        search() {
            let index = this.videos.number + 1
            this.handleCurrentChange(index)
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}
.block {
  text-align: center;
  margin-top: 1%;
}
</style>